package demo07_AuxiliaryClass;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;

/**
 * @author kunkun
 * @className Test01_CountDownLatch
 * @Description 测试加法计数器
 * @date 2025/3/6 10:48
 */
public class Test02_CyclicBarrier {

    public static void main(String[] args) throws InterruptedException {

        //1. 新建加法计数器
        CyclicBarrier cyclicBarrier = new CyclicBarrier(7, () -> {
            System.out.println("集齐7颗龙珠，召唤神龙！");
        });
        //2. 新建线程
        for (int i = 0; i < 7; i++) {
            final int temp = i+1;
            new Thread(()->{
                System.out.println("已经获得第"+temp+"颗龙珠，等待神龙召唤");
                try {
                    cyclicBarrier.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                } catch (BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
            }).start();
        }


    }

}
